package com.hottv.meeting.videomeeting.module.meeting;

import android.content.Context;
import android.media.AudioManager;
import android.os.RemoteException;
import android.view.View;

import com.hottv.meeting.videomeeting.R;
import com.hottv.meeting.videomeeting.common.MeetingConfig;
import com.hottv.meeting.videomeeting.common.MeetingUtils;
import com.hottv.meeting.videomeeting.module.agora.AgoraManager;
import com.hottv.meeting.videomeeting.module.edp.EDPUtils;
import com.hottv.meeting.videomeeting.module.meeting.bean.GetUserDetailRequest;
import com.hottv.meeting.videomeeting.module.meeting.bean.HotMessage;
import com.hottv.meeting.videomeeting.module.meeting.bean.InfoReq;
import com.hottv.meeting.videomeeting.module.meeting.view.VideoData;
import com.skw.library.focus.AbsFocusEffectView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chengwenqiang on 2019/7/15.
 */

public class MeetUtils {

    public static List<VideoData> getVideoDataList(int myUserId,boolean videoFirst)
    {
        List<VideoData> videolist = new ArrayList<>();
        AgoraManager manager = AgoraManager.getInstance();

        if(videoFirst)
        {
            videolist.add(VideoData.getWhiteData());
            videolist.addAll(manager.getVideoDataList(myUserId,videoFirst));
//            if(videolist.size()<MeetingConfig.MAX_NUM)
//            {
//                videolist.add(VideoData.getAddData());
//            }
        }
        else
        {
            videolist.addAll(manager.getVideoDataList(myUserId,videoFirst));
//            if(videolist.size()<MeetingConfig.MAX_NUM)
//            {
//                videolist.add(VideoData.getAddData());
//            }
        }
        return  videolist;
    }

    //获取用户成员
    public static String getUserType(int userType)
    {
        if(userType== MeetingConfig.BROADCASTER_TYPE)
        {
            return "主播";
        }
        if(userType==MeetingConfig.AUDIENCE_TYPE)
        {
            return "成员";
        }
        return  "";
    }


    // 获取自己信息
    public static HotMessage.UserBean getUserBean(Context context)
    {
//        String hottvId = MeetingUtils.getUnifykeys(context);
        String hottvId = MeetingUtils.getHottvId(context);
        int oneNetId =  Integer.parseInt(EDPUtils.edp_devices_Id) ;
        String nickName = MeetingManager.getInstance().getNameFromDeviceId(oneNetId);

        HotMessage.UserBean userBean = new HotMessage.UserBean();
        userBean.setHottvId(hottvId);
        userBean.setUserId(oneNetId);
        userBean.setNickname(nickName);
        userBean.setAvatar("no");
        return  userBean;
    }

    // 获取自己信息
    public  HotMessage.BroadcastBean getBroadcastBean(Context context)
    {
//        String hottvId = MeetingUtils.getUnifykeys(context);
        String hottvId = MeetingUtils.getHottvId(context);
        int oneNetId =  Integer.getInteger(EDPUtils.edp_devices_Id) ;
        String nickName = MeetingManager.getInstance().getNameFromDeviceId(oneNetId);

        HotMessage.BroadcastBean userBean = new HotMessage.BroadcastBean();
        return  userBean;
    }

    //获取聊天信息body
    public static HotMessage getMsgMessage(Context context,String msgContent)
    {
        HotMessage hotMsg =new HotMessage();
        hotMsg.setSignalType(HotMessage.MESSAGE_TYPE_MESSAGE);
        hotMsg.setContent(msgContent);

//        String hottvId = MeetingUtils.getUnifykeys(context);
        String hottvId = MeetingUtils.getHottvId(context);
        int oneNetId =  Integer.parseInt(EDPUtils.edp_devices_Id) ;
        String nickName = MeetingManager.getInstance().getNameFromDeviceId(oneNetId);

        HotMessage.MeetingBean meetingBean = new HotMessage.MeetingBean();
        meetingBean.setMeetingId("");
        meetingBean.setMeetingNickname( "");
        meetingBean.setMeetingUserId(0);
        meetingBean.setMeetingAvatar("no");
        hotMsg.setMeeting(meetingBean);

        HotMessage.BroadcastBean broadcastBean = new HotMessage.BroadcastBean();
        broadcastBean.setBroadcastId("");
        broadcastBean.setBroadcastNickname("");
        broadcastBean.setBroadcastUserId(0);
        broadcastBean.setBroadcastAvatar("no");
        hotMsg.setBroadcast(broadcastBean);

        HotMessage.UserBean userBean = new HotMessage.UserBean();
        userBean.setHottvId(hottvId);
        userBean.setUserId(oneNetId);
        userBean.setNickname(nickName);
        userBean.setAvatar("no");
        hotMsg.setUser(userBean);

        return hotMsg;
    }

    //获取请求消息
    public static HotMessage getRequestHotMessage(Context context,int signalType)
    {
        HotMessage hotMsg =new HotMessage();
        hotMsg.setSignalType(signalType);

//        String hottvId = MeetingUtils.getUnifykeys(context);
        String hottvId = MeetingUtils.getHottvId(context);
        int oneNetId =  Integer.parseInt(EDPUtils.edp_devices_Id) ;
        String nickName = MeetingManager.getInstance().getNameFromDeviceId(oneNetId);

        HotMessage.MeetingBean meetingBean = new HotMessage.MeetingBean();
        meetingBean.setMeetingId("");
        meetingBean.setMeetingNickname( "");
        meetingBean.setMeetingUserId(0);
        meetingBean.setMeetingAvatar("no");
        hotMsg.setMeeting(meetingBean);

        HotMessage.BroadcastBean broadcastBean = new HotMessage.BroadcastBean();
        broadcastBean.setBroadcastId("");
        broadcastBean.setBroadcastNickname("");
        broadcastBean.setBroadcastUserId(0);
        broadcastBean.setBroadcastAvatar("no");
        hotMsg.setBroadcast(broadcastBean);

        HotMessage.UserBean userBean = new HotMessage.UserBean();
        userBean.setHottvId(hottvId);
        userBean.setUserId(oneNetId);
        userBean.setNickname(nickName);
        userBean.setAvatar("no");
        hotMsg.setUser(userBean);

        return hotMsg;
    }

    //获取主播消息
    public static HotMessage getBroadcastHotMessage(Context context,int signalType)
    {
        HotMessage hotMsg =new HotMessage();
        hotMsg.setSignalType(signalType);

//        String hottvId = MeetingUtils.getUnifykeys(context);
        String hottvId = MeetingUtils.getHottvId(context);
        int oneNetId =  Integer.parseInt(EDPUtils.edp_devices_Id) ;
        String nickName = MeetingManager.getInstance().getNameFromDeviceId(oneNetId);

        HotMessage.MeetingBean meetingBean = new HotMessage.MeetingBean();
        meetingBean.setMeetingId(hottvId);
        meetingBean.setMeetingNickname( nickName);
        meetingBean.setMeetingUserId(oneNetId);
        meetingBean.setMeetingAvatar("no");
        hotMsg.setMeeting(meetingBean);

        HotMessage.BroadcastBean broadcastBean = new HotMessage.BroadcastBean();
        broadcastBean.setBroadcastId(hottvId);
        broadcastBean.setBroadcastNickname(nickName);
        broadcastBean.setBroadcastUserId(oneNetId);
        broadcastBean.setBroadcastAvatar("no");
        hotMsg.setBroadcast(broadcastBean);

        HotMessage.UserBean userBean = new HotMessage.UserBean();
        userBean.setHottvId(hottvId);
        userBean.setUserId(oneNetId);
        userBean.setNickname(nickName);
        userBean.setAvatar("no");
        hotMsg.setUser(userBean);

        return hotMsg;
    }
    private static  final  String AUDIO_SOUND = "AUDIO_SOUND";
    public static void   enableLocalAudio(Context context,boolean enable)
    {

        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if(enable)
        {
          int volume =   SharedPreferencesUtil.getInt(context,AUDIO_SOUND,0);
          if(volume!=0)
          {
              audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND);
          }
        }
        else
        {
            int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            SharedPreferencesUtil.putInt(context,AUDIO_SOUND,currentVolume);
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND);
        }
    }

    public static String getCurrentTime()
    {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    public static String formatTimeS(long seconds) {
        int temp = 0;
        StringBuffer sb = new StringBuffer();
        if (seconds > 3600) {
            temp = (int) (seconds / 3600);
            sb.append((seconds / 3600) < 10 ? "0" + temp + ":" : temp + ":");
            temp = (int) (seconds % 3600 / 60);
            changeSeconds(seconds, temp, sb);
        } else {
            temp = (int) (seconds % 3600 / 60);
            changeSeconds(seconds, temp, sb);
        }
        return sb.toString();
    }

    private static void changeSeconds(long seconds, int temp, StringBuffer sb) {
        sb.append((temp < 10) ? "0" + temp + ":" : "" + temp + ":");
        temp = (int) (seconds % 3600 % 60);
        sb.append((temp < 10) ? "0" + temp : "" + temp);
    }

    public static GetUserDetailRequest getUserDetailRequest(Context context)
    {
        GetUserDetailRequest req =new GetUserDetailRequest();
        GetUserDetailRequest.AuthBean auth =new GetUserDetailRequest.AuthBean();

        String  deviceId = "";
        String token ="";
        try {
            deviceId = InfoAccessImpl.getInstance(context).getInfoData(InfoAccessImpl.DEVICEID, "");
            token = InfoAccessImpl.getInstance(context).getInfoData(InfoAccessImpl.TOKEN_ID, "");
        }catch (RemoteException e)
        {
            e.printStackTrace();
        }

        String mac = MacUtils.getMacAddress(context);

        auth.setImei(deviceId);
        auth.setMobile(deviceId);
        auth.setToken(token);
        auth.setImsi(mac);
        auth.setTimestamp(String.valueOf(System.currentTimeMillis()));
        req.setAuth(auth);

        InfoReq reqCon = new InfoReq();
        reqCon.setDeviceCode(deviceId);
        req.setReq(reqCon);

        req.setApi_ver("1.0");

        return  req;

    }

    public static void addClickNormalBox(View view, boolean translate, boolean scale)
    {
        view.setFocusable(true);
        view.setTag(R.id.focus_type, AbsFocusEffectView.FocusType.FOCUS_TAB_VIEW);
        view.setTag(R.id.focus_type_is_translate, translate);
        view.setTag(R.id.focus_type_is_scale_anim, scale);
    }

}
